Skip to content
This repository has been archived by the owner on Dec 27, 2020. It is now read-only.

Add delete text action #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add delete text action #15

wants to merge 2 commits into from

Conversation

yibolu
Copy link
Collaborator

@yibolu yibolu commented Aug 25, 2020

Added delete text action and its test.
The delete text action will be used by the BACKSPACE handler, which will delete a single char if a single place gets selected, and will delete a chunk of text if they are selected.

Currently, delete action has two modes:

  1. delete single char
  2. delete multiple chars among either multiple segments/single segment.

Only #1 will be used currently since we don't have the handlers for mouse up/down, and selection service either.

@magicoder10 magicoder10 changed the title Add delete text action and its test Add delete text action Aug 25, 2020
Comment on lines 1 to +10
export function insert<T>(arr: T[], index: number, slice: T[]): T[] {
return [...arr.slice(0, index), ...slice, ...arr.slice(index)];
}

export function remove<T>(arr: T[], idx: number, count: number = 1): T[] {
return [
...arr.slice(0, idx),
...arr.slice(idx + count)
];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are going to use those helpers over & over again. Let's include unit tests for them so that we are confident they are working correctly when dealing with complexities for the actions.

}
},
{
name: 'should delete 1 character at cursor position and should delete the segment when segment is empty',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'should delete 1 character at cursor position and should delete the segment when segment is empty',
name: 'should delete 1 character and the empty segment',

}
},
{
name: 'should delete multiple characters at cursor selection',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'should delete multiple characters at cursor selection',
name: 'should delete all selected characters in a single segment',


const testCases: ITestCase[] = [
{
name: 'should delete 1 character at cursor position',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'should delete 1 character at cursor position',
name: 'should delete 1 character in a single segment',

}
},
{
name: 'should delete multiple characters at cursor selection among different start/end segment',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: 'should delete multiple characters at cursor selection among different start/end segment',
name: 'should delete multiple selected characters across 2 segments',

{},
endSegment,
{
// to remove the single char at the cursor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// to remove the single char at the cursor

let startIndex = startSegment.index;
let endIndex = endSegment.index;
if (startSegment.content.length === 0) {
startIndex = startIndex - 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
startIndex = startIndex - 1;
startIndex--;

startIndex = startIndex - 1;
}
if (endSegment.content.length === 0) {
endIndex = endIndex + 1;
Copy link
Member

@magicoder10 magicoder10 Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
endIndex = endIndex + 1;
endIndex++;

src/state/delete-text.action.ts Show resolved Hide resolved

if (endIndex - startIndex - 1 > 0) {
newSegments = remove<ISegment>(newSegments, startIndex + 1, endIndex - startIndex - 1);
newSegments = this.rematchIndex(newSegments);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
newSegments = this.rematchIndex(newSegments);
newSegments = this.reassignIndices(newSegments);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants